// pages/bindCard/bindCard.js
var utils = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        idcard: '',
        cardNumber: '',
        phone: '',
        code: '',
        sendCode: '获取验证码',
        codeId: '',
        showPopup: false,
        isClick: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    formName: function (e) {
        this.setData({
            name: e.detail.value.replace(new RegExp(" ", "gm"), "")
        })
    },
    formIdcard: function (e) {
        this.setData({
            idcard: e.detail.value.replace(new RegExp(" ", "gm"), "")
        })
    },
    formcardNumber: function (e) {
        this.setData({
            cardNumber: e.detail.value.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
        })
    },
    formPhone: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    formCode: function (e) {
        this.setData({
            code: e.detail.value.replace(new RegExp(" ", "gm"), "")
        })
    },
    getCode: function () {
        var that = this;
        var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
        var bankCardNoReg = /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/;
        if (that.data.name == '') {
            wx.showToast({
                title: '请输入持卡人姓名',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.idcard == '') {
            wx.showToast({
                title: '请输入身份证号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!idcardReg.test(that.data.idcard)) {
            wx.showToast({
                title: '请输入正确的身份证号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.cardNumber == '') {
            wx.showToast({
                title: '请输入银行卡号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!bankCardNoReg.test(that.data.cardNumber.replace(new RegExp(" ", "gm"), ""))) {
            wx.showToast({
                title: '请输入正确的银行卡号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.phone == '') {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!(that.data.phone.replace(new RegExp(" ", "gm"), "")).match(/^[1][345879][0-9]{9}$/)) {
            wx.showToast({
                title: '请输入正确手机号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.sendCode != '获取验证码') return;
        if (that.data.isClick) {
            that.setData({
                showPopup: !that.data.showPopup
            })
        }
    },
    showPop: function () {
        var that = this;
        that.setData({
            showPopup: !that.data.showPopup,
            isClick: false
        });
        wx.showLoading({
            title: '请稍等！'
        });
        utils.FetchRequest('/v1/api/bankcardAuth',
            {
                realName: that.data.name,
                idcardNo: that.data.idcard,
                bankCardNo: that.data.cardNumber.replace(new RegExp(" ", "gm"), ""),
                mobile: that.data.phone.replace(new RegExp(" ", "gm"), "")
            }).then((res) => {
            if (res.data.status == 200) {
                var countDown = 60;
                var timer = setInterval(() => {
                    if (countDown == 0) {
                        clearInterval(timer);
                        that.setData({
                            sendCode: '获取验证码',
                            isClick: true
                        })
                    } else {
                        countDown--;
                        that.setData({
                            sendCode: countDown + 's'
                        })
                    }
                }, 1000);
                that.setData({
                    codeId: res.data.data
                });
                wx.hideLoading();
            } else {
                that.setData({
                    sendCode: '获取验证码',
                    isClick: true
                });
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        })
    },
    submit: function () {
        var that = this;
        var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
        var bankCardNoReg = /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/;
        if (that.data.name == '') {
            wx.showToast({
                title: '请输入持卡人姓名',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.idcard == '') {
            wx.showToast({
                title: '请输入身份证号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!idcardReg.test(that.data.idcard)) {
            wx.showToast({
                title: '请输入正确的身份证号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.cardNumber == '') {
            wx.showToast({
                title: '请输入银行卡号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!bankCardNoReg.test(that.data.cardNumber.replace(new RegExp(" ", "gm"), ""))) {
            wx.showToast({
                title: '请输入正确的银行卡号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.phone == '') {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!(that.data.phone.replace(new RegExp(" ", "gm"), "")).match(/^[1][345879][0-9]{9}$/)) {
            wx.showToast({
                title: '请输入正确手机号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (that.data.code == '') {
            wx.showToast({
                title: '请输入验证码',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        wx.showLoading({
            title: '请稍等！'
        });
        utils.FetchRequest('/v1/api/bankcardConfirm',
            {
                realName: that.data.name,
                idcardNo: that.data.idcard,
                bankCardNo: that.data.cardNumber.replace(new RegExp(" ", "gm"), ""),
                mobile: that.data.phone.replace(new RegExp(" ", "gm"), ""),
                smsCode: that.data.code,
                cardId: that.data.codeId
            }).then((res) => {
            if (res.data.status == 200) {
                if (res.data.data) {
                    wx.setStorageSync('token', res.data.data);
                }
                wx.showToast({
                    title: '绑卡成功',
                    icon: 'none',
                    duration: 2000
                });
                setTimeout(() => {
                    wx.redirectTo({
                        url: '/pages/authentication/authentication'
                    })
                }, 2000);
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.hideHomeButton()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        wx.hideHomeButton()
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }
})